package com.epoint.mobileoa.bizlogic;



import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.exception.CrashHandler;
import com.epoint.mobileoa.http.HttpHelp;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.HttpUtil;
import com.epoint.mobileoa.tools.ToastHelp;

/**
 * @author liyc
 * @time 2011-12-28 下午6:30:51
 * @annotation 通用页面,所有Activity默认必须继承之，否则无法统一控制
 */
public abstract class SuperView extends Activity implements OnClickListener,OnItemClickListener,OnItemLongClickListener{

	public RelativeLayout mainFrame;
	public RelativeLayout topmenu;
	public int layoutpage;
	public Button top_left_bt;
	public Button top_right_bt;
	public TextView top_title;
	public ProgressBar top_pb;
	public FrameParam fp;
	public RelativeLayout rlToolBar;
	public ProgressDialog m_Dialog;
	public static AlertDialog epointDialog;
	/**
	 * 任务是否正在执行
	 */
	public boolean isTaskExecuteFlag;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//程序溃异常处理
		CrashHandler crashHandler = CrashHandler.getInstance();  
		crashHandler.init(this);
		
		//检测SD卡是否存在
		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)){
			ToastHelp.showToast(this, "请检查SD卡是否可用！");
			finish();
		}

		//消除状态栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		//强制竖屏
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		//键盘默认隐藏
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		//获取系统全局参数
		fp = (FrameParam) getApplication();

		if(fp==null){
			ToastHelp.showToast(this, "全局变量异常，建议退出软件重新登录！");
		}

		fp.getAlist().add(this);

		LayoutInflater inflater = LayoutInflater.from(this);
		layoutpage = getLayoutInt();
		mainFrame = (RelativeLayout) inflater.inflate(layoutpage, null);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);

		addContentView(mainFrame, params);

		topmenu = (RelativeLayout) inflater.inflate(R.layout.biz_topmenu, null);
		rlToolBar = (RelativeLayout) topmenu.findViewById(R.id.rlToolbar);

		top_title = (TextView) topmenu.findViewById(R.id.gp_topbar_tvtitle);
		mainFrame.addView(topmenu);

		if(!setTitle().trim().toString().equals(""))
		{
			top_title.setText(setTitle());
		}
		else if(getIntent().getStringExtra("viewtitle")!=null)
		{
			top_title.setText(getIntent().getStringExtra("viewtitle"));
		}
		else{
			top_title.setText(Html.fromHtml("<font color=red>无标题</font>"));
		}

		top_left_bt = (Button) findViewById(R.id.gp_topbar_leftbt);
		top_left_bt.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(top_pb.getVisibility()!=View.VISIBLE){
					finish();
				}
			}
		});
		top_right_bt = (Button) findViewById(R.id.gp_topbar_rightbt);
		top_pb = (ProgressBar) findViewById(R.id.gp_topbar_pbar);
		initControls();
	}

	public void executeRefresh(int TaskId,Object... param) {
		if(MainService.allNewTask.size()==0){}
		hideWaitDialog();
		top_pb.setVisibility(View.GONE);
		String bs = param[0].toString();
		if (bs.startsWith(HttpUtil.CONNECTION_TIME_OUT)||bs.startsWith(HttpHelp.CONNECTION_TIME_OUT)) {
			ToastHelp.showAndroidToastWorning(this, "网络连接超时");
		} else if (bs.startsWith(HttpUtil.INTERFACE_ERR)||bs.startsWith(HttpHelp.INTERFACE_ERR)) {
			ToastHelp.showAndroidToastWorning(this, "服务接口异常");
		} else if (bs.startsWith(HttpUtil.NO_SUCH_METHOD)||bs.startsWith(HttpHelp.NO_SUCH_METHOD)) {
			ToastHelp.showAndroidToastWorning(this, "消息发送失败");
		} else {
			refreshForNewTask(TaskId,param);
		}
		isTaskExecuteFlag = false;
	}

	public boolean synTask() {
		if (isTaskExecuteFlag) {
			// ToastHelp.showToast(this, "任务正在执行，请稍后...");
			return true;
		} else {
			return false;
		}
	}

	public void hideWaitDialog() {
		if (m_Dialog != null) {
			if (m_Dialog.isShowing()) {
				m_Dialog.dismiss();
			}
		}
	}

	public Context getContext()
	{
		return this;
	}

	public void showDialog_wait(String msg){
		msg = msg==null?"":msg;
		if(msg.equals("")){
			msg = "正在加载数据...";
		}
		m_Dialog = new ProgressDialog(this);
		m_Dialog.setMessage(msg);	
		m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		m_Dialog.setCancelable(false);
		m_Dialog.show();
	}

	public TextView getLayoutTextViewById(int id){
		TextView tv = (TextView)findViewById(id);
		setTextViewFontColor(tv);
		return tv;
	}

	public void showTopRightBtn(int bg){
		top_right_bt.setVisibility(View.VISIBLE);
		top_right_bt.setBackgroundResource(bg);
		top_right_bt.setOnClickListener(this);
	}

	public void showTopRightBtn(String btTitle){
		top_right_bt.setVisibility(View.VISIBLE);
		top_right_bt.setText(btTitle);
		top_right_bt.setOnClickListener(this);
	}

	public void showRightProgressBar(){
		top_pb.setVisibility(View.VISIBLE);
	}

	public void hideRightProgressBar(){
		top_pb.setVisibility(View.GONE);
	}

	/**
	 * 初始化控件
	 */
	public abstract void initControls();

	@Deprecated
	public abstract void refresh(Object... param) throws Exception;

	public abstract void refreshForNewTask(int TaskId,Object... param);
	/**
	 * 获取页面布局文件，注意不是通用布局文件
	 */
	public abstract int getLayoutInt();

	public void setBackground(int color) {
		if(getBackgroundStr().equals("background"))
			mainFrame.setBackgroundResource(R.drawable.background);
		else
			mainFrame.setBackgroundResource(R.drawable.white);
	}

	//设置全屏模式
	public void setScreenMode(){
		String isfullscreen_db = DBHelp.getConfigValue(this, ConfigKey.isfullscreen);
		String isfullscreen = isfullscreen_db.equals("")?"0":isfullscreen_db;
		if(isfullscreen.equals("1")){
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
		else{
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if(top_pb.getVisibility()!=View.VISIBLE){
				KEYCODE_BACK();
			}
			break;
		}
		return false;
	}

	/**
	 * 返回按钮事件
	 */
	public void KEYCODE_BACK() {
		finish();
	}

	/**
	 * 返回标题
	 */
	public String setTitle() {
		return "";
	};

	@Override
	public void onClick(View v) {

	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

	}

	//	public void handleException(Exception e){
	////		LogHelp.Log2SDErr(e);
	//		ToastHelp.showToast(this, getString(R.string.err_tip), 1000);
	//	}

	@Override
	protected void onResume() {
		super.onResume();
		setBackground(fp.getBackgroundcolor());
		if(DBHelp.getConfigValue(this, ConfigKey.backgroundcolor).equals("background")){
			topmenu.setBackgroundResource(R.drawable.toolbar_bg);
			top_left_bt.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_menu_back_selectorl));
			top_right_bt.setBackgroundResource(R.drawable.top_right_btnbg1);
		}
		else{
			topmenu.setBackgroundResource(R.drawable.top_back_image);
			top_left_bt.setBackgroundDrawable(getResources().getDrawable(R.drawable.wb_back_btn));
			top_right_bt.setBackgroundResource(R.drawable.wb_btn);
		}
		setScreenMode();
	}

	/**
	 * 获取主题字符串
	 */
	public String getBackgroundStr(){
		return DBHelp.getConfigValue(this, ConfigKey.backgroundcolor);
	}

	/**
	 * 隐藏顶部栏
	 */
	public void hideTopBar()
	{
		topmenu.setVisibility(View.GONE);
	}

	/**
	 * 获取白色或者黑色，根据主题控制
	 */
	public String getBlackOrWhiteColor(){

		if(getBackgroundStr().equals("background")){
			return "white";
		}
		else{
			return "black";
		}
	}

	/**
	 * 获取黄色或绿色，根据主题控制
	 */
	public String getYGColor(){
		if(getBackgroundStr().equals("background")){
			return "yellow";
		}
		else{
			return "green";
		}
	}

	public void setTextViewFontColor(TextView tv){
		if(getBackgroundStr().equals("background")){
			tv.setTextColor(getResources().getColor(R.color.realwhite));
		}
		else{
			tv.setTextColor(getResources().getColor(R.color.black));
		}
	}

	public void setButtonFontColor(Button tv){
		if(getBackgroundStr().equals("background")){
			tv.setTextColor(getResources().getColor(R.color.realwhite));
		}
		else{
			tv.setTextColor(getResources().getColor(R.color.black));
		}
	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		fp.getAlist().remove(this);
	}


	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);
		overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		super.startActivityForResult(intent, requestCode);
		overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		return false;
	}

}
